/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package boundary;

import entity.Event;
import entity.Participant;
import entity.ResearchUser;
import entity.User;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import security.EventBean;


/**
 *
 * @author MONESI
 */

@Stateless
@Named
public class SearchManager {
    
    @PersistenceContext
    EntityManager em;
    
   
      
    @Inject
    Principal principal;
          
    List<User> utenti;
    
    @EJB
    UserFacade uf;
    
    
    public void save(ResearchUser user) {
        
        int x;
        int n;
        List<Integer> i = em.createQuery("SELECT MAX(u.id) FROM SEARCHUSER u ").getResultList();
        
        x=i.size()-1;
        n=i.get(x);
        n++;
        user.setId(n);
        
        em.persist(user);
        
    }
    /*
    public void settaUtente(String loggedUser, String SearchedUser) {
        utenti = (Vector) uf.findAll();
        boolean trovato=false;
        
        for(int i=0;i<utenti.size() && !trovato ;i++){
            if(utenti.get(i).getEmail().equals(loggedUser)){
                trovato=true;
                utenti.get(i).setUserSearched(SearchedUser);
                uf.edit(utenti.get(i));
            }
        }
                
    }*/
    
    public String userSearched (String loggedUser){
       
        List<String> a = em.createQuery("SELECT e.searchedUser FROM SEARCHUSER e WHERE e.mail LIKE :logged").setParameter("logged", loggedUser).getResultList();
         return a.get(a.size()-1);
        
    }
    public String privacy (){
       List <String> b = em.createQuery("SELECT u.calendarPrivacy FROM USERS u JOIN SEARCHUSER s WHERE s.searchedUser = u.email ").getResultList();                   
       return b.get(b.size()-1);
    }
    
    public String name (){
       List <String> c = em.createQuery("SELECT u.name FROM USERS u JOIN SEARCHUSER s WHERE s.searchedUser = u.email ").getResultList();                   
       return c.get(c.size()-1);
    }
   
    
}


